import arcpy
import string
import os
import sys
reload(sys)
sys.setdefaultencoding('GBK')

try:
    arcpy.AddMessage('-----------start-----------')
    ws=arcpy.GetParameterAsText(0)
    outDir=arcpy.GetParameterAsText(1)
    outCSV=os.path.join(outDir,'data.csv')
    arcpy.env.workspace = ws
    rasters = arcpy.ListRasters("*")
    for raster in rasters:
        rasloc = ws + os.sep + raster
        fields = "*"
        try:
            lstFlds = arcpy.ListFields(rasloc)
            if len(lstFlds) == 0:
                break
            header='RasterName'
            for fld in lstFlds:
                header += ",{0}".format(fld.name)
            header +='\n'
            f = open(outCSV,'a')
            f.write(header)
            with arcpy.da.SearchCursor(rasloc, fields) as cursor:
                for row in cursor:
                    arcpy.AddMessage(row)
                    f.write(raster+','+str(row).replace("(","").replace(")","") + '\n')
            f.close()
        except Exception as e:
            print (e)
    del row
    arcpy.AddMessage('-----------end-----------')
except arcpy.ExecuteError:
    print arcpy.GetMessages()